//
//  XHSegmentControl.m
//  HarmonizePub
//
//  Created by Haven on 2016/11/3.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "XHSegmentControl.h"
#import "Config.h"

@interface XHSegmentControl()

@property (nonatomic, strong) UISegmentedControl *segMentControl;

@property (nonatomic, strong) UIView *lineView;

// items数量
@property (nonatomic, assign) NSInteger segmentCount;

@end



@implementation XHSegmentControl

#define viewW  self.bounds.size.width
#define viewH  self.bounds.size.height
#define lineH  2
#define lineX  17




- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
}

-(void)setItemsArray:(NSArray *)itemsArray
{
    _itemsArray = itemsArray;
    // 创建控件
    [self createSegmentControl: itemsArray.count];
}

- (void)createSegmentControl: (NSInteger)count
{
    // 下划线
//    LRLog(@"%d", lineX);
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(lineX, viewH - lineH - 3, viewW / count - lineX * 2, lineH)];
    self.lineView.backgroundColor = XHConfigColor;
    [self addSubview: self.lineView];
    
    // segmentControl
    self.segMentControl = [[UISegmentedControl alloc] initWithItems: self.itemsArray];
    
    self.segMentControl.frame = CGRectMake(0, 0, viewW, viewH - lineH - 3);
    
    self.segMentControl.tintColor = [UIColor clearColor];
    
    self.segMentControl.selectedSegmentIndex = 0;
    
    [self.segMentControl addTarget:self action:@selector(selectSegmentControl:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview: _segMentControl];
}

// 选中事件
- (void)selectSegmentControl: (UISegmentedControl *)segmentControl
{
    // 实现动画
    [self lineViewAnimotion: segmentControl.selectedSegmentIndex];
    
    // 实现代理方法
    if ([self.delagate respondsToSelector:@selector(selectSegmentControlWithIndex:)]) {
        [self.delagate selectSegmentControlWithIndex: segmentControl.selectedSegmentIndex];
    }
}

- (void)lineViewAnimotion: (NSInteger)index
{
    [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping: 0.7 initialSpringVelocity: 1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CGRect lineFrame = self.lineView.frame;
        lineFrame.origin.x = lineX + viewW / self.itemsArray.count * index;
        self.lineView.frame = lineFrame;
        [self setNeedsLayout];
    } completion:^(BOOL finished) {
    }];

}


- (void)setSegmentWithNormalColor:(UIColor *)normalColor selectColor:(UIColor *)selectColor fontSize:(CGFloat)fontSize
{
    NSDictionary *normalAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize: fontSize], NSForegroundColorAttributeName : normalColor};
    [self.segMentControl setTitleTextAttributes: normalAttributes forState: UIControlStateNormal];

    NSDictionary *selectAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize: fontSize], NSForegroundColorAttributeName : selectColor};
    [self.segMentControl setTitleTextAttributes: selectAttributes forState: UIControlStateSelected];
}


// 修改index
- (void)changeSegmentControlIndex: (NSInteger)index
{
    self.segMentControl.selectedSegmentIndex = index;
    // 实现动画
    [self lineViewAnimotion: index];
}
@end
